package com.ruoyi.record.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.basic.domain.BasicImage;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.domain.RecordFile;
import com.ruoyi.record.service.IRecordFileService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-19
 */
@Controller
@RequestMapping("/record/file")
public class RecordFileController extends BaseController
{
    private String prefix = "record/file";

    @Autowired
    private IRecordFileService recordFileService;

    @RequiresPermissions("record:file:view")
    @GetMapping()
    public String file()
    {
        return prefix + "/file";
    }

    /**
     * 查询文件管理列表
     */
    @RequiresPermissions("record:file:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordFile recordFile)
    {
        startPage();
        List<RecordFile> list = recordFileService.selectRecordFileList(recordFile);
        return getDataTable(list);
    }

    /**
     * 新增文件管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文件管理
     */
    @RequiresPermissions("record:file:add")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam MultipartFile file, RecordFile recordFile) throws IOException {
        if(!file.isEmpty()) {
            String fileUrl = FileUploadUtils.upload(RuoYiConfig.getFilePath(), file, true);
            recordFile.setFileUrl(fileUrl);
        }
        recordFile.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recordFileService.insertRecordFile(recordFile));
    }

    /**
     * 修改文件管理
     */
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") Long fileId, ModelMap mmap)
    {
        RecordFile recordFile = recordFileService.selectRecordFileByFileId(fileId);
        mmap.put("recordFile", recordFile);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件管理
     */
    @RequiresPermissions("record:file:edit")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam(required = false) MultipartFile file, RecordFile recordFile) throws IOException {
        if(!file.isEmpty()) {
            RecordFile old = recordFileService.selectRecordFileByFileId(recordFile.getFileId());
            FileUtils.deleteFile(RuoYiConfig.getProfile() + StringUtils.substringAfter(old.getFileUrl(), Constants.RESOURCE_PREFIX));

            String fileUrl = FileUploadUtils.upload(RuoYiConfig.getFilePath(), file, true);
            recordFile.setFileUrl(fileUrl);
        }
        recordFile.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recordFileService.updateRecordFile(recordFile));
    }

    /**
     * 删除文件管理
     */
    @RequiresPermissions("record:file:remove")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        for(Long id : Convert.toLongArray(ids)) {
            RecordFile old = recordFileService.selectRecordFileByFileId(id);
            FileUtils.deleteFile(RuoYiConfig.getProfile() + StringUtils.substringAfter(old.getFileUrl(), Constants.RESOURCE_PREFIX));
        }
        return toAjax(recordFileService.deleteRecordFileByFileIds(ids));
    }
}
